package com.zj.leetcode.easy.greed;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description: 汇总区间
 * 228 summary-ranges
 * @author: Zhang Bo
 * @create: 2022-03-25 10:34:40
 **/
public class SummaryRanges {
    public static void main(String[] args) {
        Solution solution = new SummaryRanges().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<String> summaryRanges(int[] nums) {
            if(nums.length == 0){
                return new ArrayList<>();
            }
            int l = 0;
            int r = 0;
            int pre = nums[0];
            ArrayList<String> list = new ArrayList<>();
            while (r < nums.length) {
                if (r - l != nums[r] - nums[l]) {
                    if(r - l == 1){
                        list.add(String.valueOf(pre));
                    } else {
                        list.add(pre + "->" + nums[r - 1]);
                    }
                    l = r;
                    pre = nums[r];
                }
                r++;
            }

            if(r - l == 1){
                list.add(String.valueOf(pre));
            } else {
                list.add(pre + "->" + nums[r - 1]);
            }
            return list;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
